package xj.graph2d.shapes;

import java.awt.geom.Point2D;
import java.awt.geom.QuadCurve2D;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;

import xj.util.gui.Point2DFloat;
import xj.util.gui.Rectangle2DFloat;

/**
 * A quadratic parametric curve segment specified with <code>float</code>
 * coordinates.
 */

public class QuadCurve2DFloat extends QuadCurve2D implements Serializable {

  /**
         * The x coordinate of the start point of the quadratic curve segment.
         */
  public float x1;

  /**
         * The y coordinate of the start point of the quadratic curve segment.
         */
  public float y1;

  /**
         * The x coordinate of the control point of the quadratic curve segment.
         */
  public float ctrlx;

  /**
         * The y coordinate of the control point of the quadratic curve segment.
         */
  public float ctrly;

  /**
         * The x coordinate of the end point of the quadratic curve segment.
         */
  public float x2;

  /**
         * The y coordinate of the end point of the quadratic curve segment.
         */
  public float y2;

  /**
         * Constructs and initializes a <code>QuadCurve2D</code> with
         * coordinates (0, 0, 0, 0, 0, 0).
         */
  public QuadCurve2DFloat() {
  }

  /**
         * Constructs and initializes a <code>QuadCurve2D</code> from the
         * specified coordinates.
         * 
         * @param x1,&nbsp;y1
         *                the starting point coordinates
         * @param ctrlx,&nbsp;ctrly
         *                the coordinates of the control point
         * @param x2,&nbsp;y2
         *                the ending point coordinates
         */
  public QuadCurve2DFloat(float x1, float y1, float ctrlx, float ctrly,
      float x2, float y2) {
    setCurve(x1, y1, ctrlx, ctrly, x2, y2);
  }

  /**
         * Returns the x coordinate of the start point in <code>double</code>
         * precision.
         * 
         * @return the x coordinate of the starting point.
         */
  @Override
  public double getX1() {
    return x1;
  }

  /**
         * Returns the y coordinate of the start point in <code>double</code>
         * precision.
         * 
         * @return the y coordinate of the starting point.
         */
  @Override
  public double getY1() {
    return y1;
  }

  /**
         * Returns the start point.
         * 
         * @return a {@link Point2D} object that is the starting point of this
         *         <code>QuadCurve2D</code>.
         */
  @Override
  public Point2D getP1() {
    return new Point2DFloat(x1, y1);
  }

  /**
         * Returns the x coordinate of the control point in <code>double</code>
         * precision.
         * 
         * @return the x coordinate of the control point.
         */
  @Override
  public double getCtrlX() {
    return ctrlx;
  }

  /**
         * Returns the y coordinate of the control point in <code>double</code>
         * precision.
         * 
         * @return the y coordiante of the control point.
         */
  @Override
  public double getCtrlY() {
    return ctrly;
  }

  /**
         * Returns the control point.
         * 
         * @return a <code>Point2D</code> that is the control point of this
         *         <code>QuadCurve2D</code>.
         */
  @Override
  public Point2D getCtrlPt() {
    return new Point2DFloat(ctrlx, ctrly);
  }

  /**
         * Returns the x coordinate of the end point in <code>double</code>
         * precision.
         * 
         * @return the x coordinate of the end point.
         */
  @Override
  public double getX2() {
    return x2;
  }

  /**
         * Returns the y coordinate of the end point in <code>double</code>
         * precision.
         * 
         * @return the y coordiante of the end point.
         */
  @Override
  public double getY2() {
    return y2;
  }

  /**
         * Returns the end point.
         * 
         * @return a <code>Point2D</code> that is the end point of this
         *         <code>QuadCurve2D</code>.
         */
  @Override
  public Point2D getP2() {
    return new Point2DFloat(x2, y2);
  }

  /**
         * Sets the location of the endpoints and controlpoint of this
         * <code>QuadCurve2D</code> to the specified <code>double</code>
         * coordinates.
         * 
         * @param x1,&nbsp;y1
         *                the coordinates of the starting point
         * @param ctrlx,&nbsp;ctrly
         *                the coordinates of the control point
         * @param x2,&nbsp;y2
         *                the coordinates of the ending point
         */
  @Override
  public void setCurve(double x1, double y1, double ctrlx, double ctrly,
      double x2, double y2) {
    this.x1 = (float) x1;
    this.y1 = (float) y1;
    this.ctrlx = (float) ctrlx;
    this.ctrly = (float) ctrly;
    this.x2 = (float) x2;
    this.y2 = (float) y2;
  }

  /**
         * Sets the location of the endpoints and controlpoint of this curve to
         * the specified <code>float</code> coordinates.
         * 
         * @param x1,&nbsp;y1
         *                the coordinates of the starting point
         * @param ctrlx,&nbsp;ctrly
         *                the coordinates of the control point
         * @param x2,&nbsp;y2
         *                the coordinates of the ending point
         */
  public void setCurve(float x1, float y1, float ctrlx, float ctrly, float x2,
      float y2) {
    this.x1 = x1;
    this.y1 = y1;
    this.ctrlx = ctrlx;
    this.ctrly = ctrly;
    this.x2 = x2;
    this.y2 = y2;
  }

  /**
         * Returns the bounding box of this <code>QuadCurve2D</code>.
         * 
         * @return a {@link Rectangle2D} that is the bounding box of the shape
         *         of this <code>QuadCurve2D</code>.
         */
  public Rectangle2D getBounds2D() {
    float left = Math.min(Math.min(x1, x2), ctrlx);
    float top = Math.min(Math.min(y1, y2), ctrly);
    float right = Math.max(Math.max(x1, x2), ctrlx);
    float bottom = Math.max(Math.max(y1, y2), ctrly);
    return new Rectangle2DFloat(left, top, right - left, bottom - top);
  }

}
